#include "View.h"


// Constructors

View::View()
{
	
}

View::View(char* imagePath)
{
	img=GlobalFunctions::LoadImageFile(imagePath);
	imgName=imagePath;
	cornersNumber=-1;
	interestPointsNumber=-1;
	imgHeght=img->height;
	imgWidth=img->width;

	cout << " constructed \n" <<imgName;
}

View::View(IplImage *image, int idframe)
{
	img=cvCloneImage(image);
	cornersNumber=-1;
	imgName="";
	interestPointsNumber=-1;
	imgHeght=img->height;
	imgWidth=img->width;
	img->ID=idframe;

	//cout << " constructed \n" <<imgName;

}



// destructor
View::~View(void)
{
	cvReleaseImage(&img);
	cout << " destructor \n";
}


void View::setImg(IplImage *image, int idframe)
{
	img=cvCloneImage(image);
	cornersNumber=-1;
	imgName="";
	interestPointsNumber=-1;
	imgHeght=img->height;
	imgWidth=img->width;
	img->ID=idframe;

	//cout << " constructed \n" <<imgName;

}


// display the image in a Frame
void View::showImage()
{
	GlobalFunctions::DisplayImage(imgName+img->ID,img);
	
}

void View::hold(){
GlobalFunctions::HoldImages();
}

// Get the image corners using Harris Corner Method
void View::GetCorners()
{
	
	printf("imqeScene = %i ,%i",img->width,img->height);
	// call the opencv method to get the image corners
	GlobalFunctions::GetImageCorners(img,CornerPoints,cornerValues,&cornersNumber);
	// Sort the corner values by high value
	sortCornerPoints();
	
}

// Mark the corners in the Image
void View::MarkImageCornes()
{
	if(cornersNumber!=-1){
		GlobalFunctions::MarkCorners(CornerPoints,&img,cornersNumber);
	}else{
		printf("\n There is no corners loaded.\n Call the function GetCorners before use MarkImageCornes ");
	}
}

// Mark the interest points in the image
void View::markInterestPoints()
{
	if(interestPointsNumber!=-1){
		GlobalFunctions::MarkCorners(interestPoints,&img,interestPointsNumber);
	}else{
		printf("\n There is no Interest Points loaded");
	}
}

// Mark 1 point in the image
void View::markPoint(CvPoint cPoint)
{
	if(cornersNumber!=-1){

		CvPoint Point[1];
		Point[0]=cPoint;
		GlobalFunctions::MarkCorners(Point,&img,1);
	}else{
		printf("\n There is no corner points detected");
	}
}



void View::printValuePoints(int numPoints)
{
	if(cornersNumber!=-1){
		 printf("\n First %i corners, sorted:\n",numPoints);
		for (int i=0; i<numPoints; i++) printf(" %0.9f \n ", cornerValues[i]);
		printf("\n");
	}else{
		printf("\n There is no corners loaded.\n Call the function GetCorners before use MarkImageCornes ");
	}
	
	
}


// Sort the corners using Quicksort

void View::sortCornerPoints()
{
	if(cornersNumber!=-1){
		GlobalFunctions::QuickSort(cornerValues,0,cornersNumber-1,CornerPoints);
	}else{
		printf("\n There is no corners loaded.\n Call the function GetCorners before use MarkImageCornes ");
	}
	
	
}



// Find and store the points of interest the the region rect
void View::setInterestPoints(CvRect rect){

if(cornersNumber!=-1){
		
	   int k=0;

		for(int i=0; i<cornersNumber;i ++){;
			
			// Verify if the points are inside the rect
			if ((CornerPoints[i].x>=rect.x && CornerPoints[i].x<=(rect.x+rect.width)) &&
				(CornerPoints[i].y>=rect.y && CornerPoints[i].y<=(rect.y+rect.height))){

					interestPoints[k]=CornerPoints[i];
					k++;
					//printf("(%i,%i) \n",CornerPoints[i].x,CornerPoints[i].y);
			}
		}
		// register the number of points in the points
		interestPointsNumber=k;
		printf("\n Number of Interest points : %i \n",k);

	}else{
		printf("\n There is no corners loaded.\n Call the function 'GetCorners' before use 'setInterestPoints' ");
	}

}